package org.youyee.game.utils.bitmapCacheUtils
{
	import com.qzone.corelib.animations.BitmapAnimationCache;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 描述一个缓冲帧 
	 * @author youyeelu
	 * 
	 */	
	public class BitmapCacheFrame
	{
		/**
		 * 位图数据 
		 */		
		public var bitmapData:BitmapData;
		
		/**
		 * 位置坐标
		 */		
		public var positionPoint:Point;
		
		/**
		 * 构造函数 
		 * @param bitmapData 位图数据
		 * @param positionPoint 位置坐标
		 * 
		 */		
		public function BitmapCacheFrame(bitmapData:BitmapData, positionPoint:Point)
		{
			this.bitmapData = bitmapData;
			this.positionPoint = positionPoint;
		}
		
		/**
		 * 销毁帧 
		 * 
		 */		
		public function dispose():void
		{
			bitmapData && bitmapData.dispose();
			positionPoint = null;
			bitmapData = null;
		}
		
		/**
		 * 创造一个缓存帧 
		 * @param movieClip 需要被缓存的MovieClip
		 * @return 返回创建好的帧
		 * 
		 */		
		public static function buildCacheFrame(movieClip:MovieClip):BitmapCacheFrame
		{
			var rect:Rectangle;
			var offsetX:Number, offsetY:Number, w:Number, h:Number;
			var matrix:Matrix;
			var bitmapData:BitmapData;
			
			rect = movieClip.getRect(movieClip);
			offsetX = -rect.x;
			offsetY = -rect.y;
			w = rect.width;
			h = rect.height;
			
			matrix = new Matrix();
			matrix.createBox(1, 1, 0, offsetX, offsetY);
			
			bitmapData = new BitmapData(w, h, true, 0);
			bitmapData.draw(movieClip, matrix);
			
			var frame:BitmapCacheFrame = new BitmapCacheFrame(bitmapData, new Point(rect.x, rect.y));
			return frame;
		}
	}
}